package com.gxa.modules.service.impl;

import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.PageRecv;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.ParameterMapper;
import com.gxa.modules.pojo.Credentials;
import com.gxa.modules.pojo.Loan;
import com.gxa.modules.pojo.Reconciliation;
import com.gxa.modules.service.ParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ParameterServiceImpl implements ParameterService {
    @Autowired(required = false)
    private ParameterMapper parameterMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public JsonResult selectPrincipalFinancial(PageRecv pageRecv) {
        JsonResult jsonResult=new JsonResult();
        Integer param1=(pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        pageRecv.setCurrentPage(param1);
            List<Loan> loanList= parameterMapper.selectPrincipal(pageRecv);
            jsonResult.setData(loanList);
        return jsonResult;
    }

    @Override
    @Cacheable(value = "selectReconciliationInformation",key = "#reconciliation_id")
    public JsonResult selectReconciliationInformation(long reconciliation_id) {
       JsonResult jsonResult=new JsonResult();
        Reconciliation reconciliation=parameterMapper.selectReconById(reconciliation_id);
        jsonResult.setCode(200);
        jsonResult.setData(reconciliation);
        jsonResult.setMsg("查询成功");
        return jsonResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CachePut(value = "selectReconciliationInformation",key = "#reconciliation.reconciliation_id")
    public JsonResult updateReconciliation(Reconciliation reconciliation) {
            JsonResult jsonResult=new JsonResult();
        parameterMapper.updateReconciliation(reconciliation);
        for (Credentials credentials:reconciliation.getCredentialsList()){
            parameterMapper.addCredentials(credentials);
        }
        jsonResult=selectReconciliationInformation(reconciliation.getReconciliation_id());
        return jsonResult;
    }

    @Override
    public JsonResult selectCollection(PageRecv pageRecv) {
        JsonResult jsonResult=new JsonResult();
       List<Loan> loanList= parameterMapper.selectCollection(pageRecv);
       jsonResult.setCode(200);
       jsonResult.setMsg("查询成功");
       jsonResult.setData(loanList);
        return jsonResult;
    }

    @Override
    public JsonResult updateCollectionAll(List<Integer> ids, String name) {
        JsonResult jsonResult=new JsonResult();
        for(Integer id:ids){
            parameterMapper.updateReconciliationAll(id,name);
        }
        jsonResult.setCode(200);
        jsonResult.setMsg("批量更新成功");
        return jsonResult;
    }
}
